package com.bawei.lookfor.view

import android.os.Parcel
import android.os.Parcelable
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.ListFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lookfor.R
import com.bawei.lookfor.databinding.ActivityMainBinding
import com.bawei.module_base.net.util.MMKVUtil
import com.bawei.module_base.ui.base.BaseActivity
import com.bawei.module_base.ui.base.BaseViewModel
import com.bawei.module_live.view.fragment.LiveFragment
import com.bawei.module_my.view.fragment.MyFragment
import com.bawei.module_nearby.NearbyFragment
import com.bawei.module_wide.view.fragment.WideFragment
import com.google.android.material.tabs.TabLayout


@Route(path = "/main/main_activity")
private class MainActivity() : BaseActivity<BaseViewModel, ActivityMainBinding>(), Parcelable {
    val image= mutableListOf<Int>(R.mipmap.app_square_0,R.mipmap.app_position_page_0,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected_0,R.mipmap.app_my_choice_0)
    val defaultImage= mutableListOf<Int>(R.mipmap.app_square,R.mipmap.app_position_page_0,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected_0,R.mipmap.app_my_choice_0)
    val selectImage= mutableListOf<Int>(R.mipmap.app_square,R.mipmap.app_position_page,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected,R.mipmap.app_my_choice)
    val titleArr= mutableListOf<String>("广场","附近","","榜单","我的")
    var isLogin:Boolean=false

    constructor(parcel: Parcel) : this() {
        isLogin = parcel.readByte() != 0.toByte()
    }

    override fun getLayout(): Int {
        return R.layout.activity_main
    }
    override fun initData() {
        isLogin = MMKVUtil.getIsLogin()
        if (!isLogin){
            ARouter.getInstance().build("/login/login_activity").navigation()
            finish()
        }
        //创建fragment集合
        val fragment= mutableListOf(
            WideFragment(), NearbyFragment(), LiveFragment(),
            ListFragment(), MyFragment()
        )
        //预加载
        binding.mainVp.offscreenPageLimit=fragment.size-1
        binding.mainVp.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            //集合总数量
            override fun getCount()=fragment.size
            //fragment布局
            override fun getItem(position: Int)=fragment[position]
            //标题
            override fun getPageTitle(position: Int)= titleArr[position]
        }
        //绑定viewPage
        binding.mainTb.setupWithViewPager(binding.mainVp)
        //默认初始化
        for ((index, i) in defaultImage.withIndex()) {
            binding.mainTb.getTabAt(index)?.setIcon(defaultImage[index])
        }

        binding.mainTb.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                //选中监听
                if (tab != null) {
                    tab.setIcon(selectImage[tab.position])
                }
            }
            //未选中监听
            override fun onTabUnselected(tab: TabLayout.Tab?) {
                if (tab != null) {
                    tab.setIcon(image[tab.position])
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })

    }

    override fun getViewModelClass(): Class<BaseViewModel> {
        return BaseViewModel::class.java
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeByte(if (isLogin) 1 else 0)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<MainActivity> {
        override fun createFromParcel(parcel: Parcel): MainActivity {
            return MainActivity(parcel)
        }

        override fun newArray(size: Int): Array<MainActivity?> {
            return arrayOfNulls(size)
        }
    }

}